package tq.com.tqteacher.optimize.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.scwang.smartrefresh.layout.constant.RefreshState;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.recyclerAdapter.declear.IRecyclerSection;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.activity.TQEmptyActivity;
import tq.com.tqteacher.activity.newPublish.TQWKLeaveMsg;
import tq.com.tqteacher.adapt.TQHomePublishAdapter;
import tq.com.tqteacher.discuss.activity.TQDiscussMngActivity;
import tq.com.tqteacher.fragment.TQHomePublishFragment;
import tq.com.tqteacher.model.TQCommentOverInfoModel;
import tq.com.tqteacher.model.TQLookerOverInfo;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishItem;
import tq.com.tqteacher.model.TQPublishSection;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.net.TQNetInterface;

public class ActivityOptimizeDetail extends BaseTitleActivity {
    private TQOptimizeFragment mOptimizeFragment = new TQOptimizeFragment();


    public static void show(Activity pActivity){
        Intent intent = new Intent(pActivity, ActivityOptimizeDetail.class);
        pActivity.startActivity(intent);
    }

    @Override
    protected BaseFragment fragment() {
        return mOptimizeFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("优化");
    }

    static public class TQOptimizeFragment extends BaseRecyclerFragment{
        private TQHomePublishAdapter mTQHomePublishAdapter;

        @Override
        protected void initRecycleView(RecyclerView pRecyclerView) {
            super.initRecycleView(pRecyclerView);

            if(mTQHomePublishAdapter == null){
                mTQHomePublishAdapter = new TQHomePublishAdapter(this.getContext(), 1);
                mTQHomePublishAdapter.setClickListener(mClickListener);
            }

            pRecyclerView.setAdapter(mTQHomePublishAdapter);
            pRecyclerView.setLayoutManager(new LinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false));

            getLinkDetail();
        }


        private TQHomePublishAdapter.IClickListener mClickListener = new TQHomePublishAdapter.IClickListener() {
            @Override
            public void arrageLeaveMsg(TQPublishItem pItem) {
                setFrom(pItem.getNewPublish());

                if(pItem.getPublishType() == TQPublishType.TYPE_FXJ
                        || pItem.getPublishType() == TQPublishType.TYPE_TB){
                    gotoLeaveMsgManager();
                }else{
                    Intent lIntent = new Intent(TQOptimizeFragment.this.getContext(), TQWKLeaveMsg.class);
                    startActivity(lIntent);
                }
            }

            @Override
            public void arragQuestion(TQPublishItem pItem) {
                Log.d(TAG, "arragQuestion: ");
                setFrom(pItem.getNewPublish());

                if(pItem.getNewPublish().getType() == TQPublishType.TYPE_TWDY){
                    Intent lIntent = new Intent(TQOptimizeFragment.this.getContext(), TQWKLeaveMsg.class);
                    startActivity(lIntent);
                }else{
                    TQEmptyActivity.show(TQOptimizeFragment.this.getActivity());
                }

            }

            @Override
            public void dataStatices(TQPublishItem pItem) {
                setFrom(pItem.getNewPublish());

                TQEmptyActivity.show(TQOptimizeFragment.this.getActivity());
            }

            @Override
            public void arrageContent(TQPublishItem pItem) {
                Log.d(TAG, "arrageContent: ");
                setFrom(pItem.getNewPublish());

                TQEmptyActivity.show(TQOptimizeFragment.this.getActivity());
            }

            @Override
            public void newLinker(TQPublishSection pItem) {

            }

            public void showViewer(TQPublishItem pItem){}
            public void showLeverMsg(TQPublishItem pItem){}
            public void showDetail(TQPublishItem pItem){
                Log.d(TAG, "showDetail: ");
                if(pItem == null) return;

                setFrom(pItem.getNewPublish());

                TQPublishType.showResult(TQOptimizeFragment.this.getActivity());
            };
        };

        private void setFrom(TQNewPublish pPublish){
            TQPublishType.setTQOpenFrom(TQPublishType.TQOpenFrom.optimize);
            TQPublishType.setSharePublishItem(pPublish);
        }
        public void gotoLeaveMsgManager(){
            Intent inten  = new Intent(getContext(), TQDiscussMngActivity.class);
            startActivity(inten);
        }

        public void getLinkDetail(){
            TQLoadingHelper.build(TQOptimizeFragment.this.getActivity()).setCancelable(false).show();

            TQNetInterface.getMaterialLink(TQPublishType.getSharePublishItem().getSeq_id(), new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        TQLoadingHelper.setError(pNetResult.getErrMsg());
                        return;
                    }

                    try {
                        if(pNetResult.getData() == null) return;
                        JSONArray all = pNetResult.getData().optJSONArray("material");
                        if(all == null) return;

                        ArrayList<TQNewPublish> lNewPublishes = new ArrayList<>();
                        for (int i = 0; i < all.length(); i++){
                            JSONObject lObject = all.optJSONObject(i);
                            if(lObject == null)continue;

                            JSONObject lPublish = lObject.optJSONObject("material");
                            int category = lPublish.optInt("category");

                            if(!TQPublishType.isValidType(category)){
                                continue;
                            }

                            TQNewPublish lPublish1 = TQPublishType.getNewPublishByType(category);
                            lPublish1.fromJson(lPublish);

                            TQCommentOverInfoModel lOverInfoModel = new TQCommentOverInfoModel(lObject);
                            lPublish1.setCommentOverInfoModel(lOverInfoModel);

                            TQLookerOverInfo lLookerOverInfo = new TQLookerOverInfo(lObject);
                            lPublish1.setLookerOverInfo(lLookerOverInfo);

                            lNewPublishes.add(lPublish1);
                        }

                        if(lNewPublishes.size() > 0){
                            mkGroup(lNewPublishes);
                        }
                    }finally {
                        TQLoadingHelper.dimiss();
                    }
                }
            });
        }

        private void mkGroup(ArrayList<TQNewPublish> pNewPublishes){

            TQPublishSection lPublishSection = null;
            for (TQNewPublish pulish:
                    pNewPublishes) {
                TQPublishItem lItem = new TQPublishItem();
                lItem.setNewPublish(pulish);

                if(lPublishSection == null){
                    lPublishSection = new TQPublishSection();
                    lPublishSection.hideFooter();
                    lPublishSection.setHeaderItem(lItem);
                }else{
                    lPublishSection.addItem(lItem);
                }

            }

            if(lPublishSection != null && lPublishSection.getiItemCount() > 0){
                mTQHomePublishAdapter.clear();
                mTQHomePublishAdapter.addSection(lPublishSection);
            }
        }
    }

}
